const { SCRIPT_URL, STYLE_URL } = require('../config');
const { httpGet, httpPost } = require('../libs/http');

class TodoController {
  constructor() {
    TodoController.funcBind(this);
  }

  static funcBind(vm) {
    vm.listView = vm.listView.bind(vm);
    vm.detailView = vm.detailView.bind(vm);
  }

  //首页列表页面的路由处理函数
  async listView(req, res) {
    const json = await httpGet('http://localhost:8000/get_todolist');
    const todoList = json.data;
    res.render('index', {
      todoList,
      scripts: SCRIPT_URL.INDEX,
      styles: STYLE_URL.INDEX
    });
  }

  //列表页列表页面的路由处理函数
  async detailView(req, res) {
    const id = req.params.id;
    const json = await httpPost('http://localhost:8000/get_todo', { id });
    const todo = json.data;
    res.render('detail', {
      todo,
      scripts: SCRIPT_URL.DETAIL,
      styles: STYLE_URL.DETAIL
    });
  }
}

module.exports = TodoController;
